package com.csproj.system.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * 角色信息
 */
@Entity
@Table(name = "sys_role_info")
@EntityListeners(AuditingEntityListener.class)
public class RoleInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid")
    @Column(name = "id")
    private String id;

    @Column(name = "name")
    private String name;                    //角色名称

    @Column(name="sort")
    private Integer sort;                   //排序

    public Boolean getUsedFlag() {
        return usedFlag;
    }

    public void setUsedFlag(Boolean usedFlag) {
        this.usedFlag = usedFlag;
    }

    @Column(name="used_flag")
    private Boolean usedFlag;

    @Column(name="remark")
    private String remark;                  //备注

    @Column(name="organization_id")
    private String organizationId;

    @Column(name = "deleted_flag")
    private Boolean deletedFlag;

    @ManyToMany
    @JoinTable(name = "sys_role_info_authority_info",
            joinColumns = @JoinColumn(name="role_infos_id", referencedColumnName="id"),
            inverseJoinColumns = @JoinColumn(name="authority_infos_id", referencedColumnName="id"))
    private Set<AuthorityInfo> authorityInfos = new HashSet<>();

    @ManyToMany(mappedBy = "roleInfos")
    @JsonIgnore
    private Set<UserInfo> userInfos = new HashSet<>();

    public Set<AuthorityInfo> getAuthorityInfos() {
        return authorityInfos;
    }

    public RoleInfo authorityInfos(Set<AuthorityInfo> authorityInfos) {
        this.authorityInfos = authorityInfos;
        return this;
    }

    public RoleInfo addAuthorityInfo(AuthorityInfo authorityInfo) {
        this.authorityInfos.add(authorityInfo);
        authorityInfo.getRoleInfos().add(this);
        return this;
    }

    public RoleInfo removeAuthorityInfo(AuthorityInfo authorityInfo) {
        this.authorityInfos.remove(authorityInfo);
        authorityInfo.getRoleInfos().remove(this);
        return this;
    }

    public void setAuthorityInfos(Set<AuthorityInfo> authorityInfos) {
        this.authorityInfos = authorityInfos;
    }

    public Set<UserInfo> getUserInfos() {
        return userInfos;
    }

    public RoleInfo userInfos(Set<UserInfo> userInfos) {
        this.userInfos = userInfos;
        return this;
    }

    public RoleInfo addUserInfo(UserInfo userInfo) {
        this.userInfos.add(userInfo);
        userInfo.getRoleInfos().add(this);
        return this;
    }

    public RoleInfo removeUserInfo(UserInfo userInfo) {
        this.userInfos.remove(userInfo);
        userInfo.getRoleInfos().remove(this);
        return this;
    }

    public void setUserInfos(Set<UserInfo> userInfos) {
        this.userInfos = userInfos;
    }

    /**
     * 删除人
     */
    @ApiModelProperty(value = "删除人")
    @Column(name = "deleter_user_id")
    private String deleterUserId;

    /**
     * 删除时间
     */
    @ApiModelProperty(value = "删除时间")
    @Column(name = "deletion_time")
    private ZonedDateTime deletionTime;

    /**
     * 最近更新时间
     */
    @LastModifiedDate
    @ApiModelProperty(value = "最近更新时间")
    @Column(name = "last_modification_time")
    private ZonedDateTime lastModificationTime;

    /**
     * 最近更新人
     */
    @LastModifiedBy
    @ApiModelProperty(value = "最近更新人")
    @Column(name = "last_modifier_user_id")
    private String lastModifierUserId;

    /**
     * 创建时间
     */
    @CreatedDate
    @ApiModelProperty(value = "创建时间")
    @Column(name = "creation_time")
    private ZonedDateTime creationTime;

    /**
     * 创建人
     */
    @CreatedBy
    @ApiModelProperty(value = "创建人")
    @Column(name = "creator_user_id")
    private String creatorUserId;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getOrganizationId() {
        return organizationId;
    }

    public void setOrganizationId(String organizationId) {
        this.organizationId = organizationId;
    }

    public Boolean getDeletedFlag() {
        return deletedFlag;
    }

    public void setDeletedFlag(Boolean deletedFlag) {
        this.deletedFlag = deletedFlag;
    }

    public String getDeleterUserId() {
        return deleterUserId;
    }

    public void setDeleterUserId(String deleterUserId) {
        this.deleterUserId = deleterUserId;
    }

    public ZonedDateTime getDeletionTime() {
        return deletionTime;
    }

    public void setDeletionTime(ZonedDateTime deletionTime) {
        this.deletionTime = deletionTime;
    }

    public ZonedDateTime getLastModificationTime() {
        return lastModificationTime;
    }

    public void setLastModificationTime(ZonedDateTime lastModificationTime) {
        this.lastModificationTime = lastModificationTime;
    }

    public String getLastModifierUserId() {
        return lastModifierUserId;
    }

    public void setLastModifierUserId(String lastModifierUserId) {
        this.lastModifierUserId = lastModifierUserId;
    }

    public ZonedDateTime getCreationTime() {
        return creationTime;
    }

    public void setCreationTime(ZonedDateTime creationTime) {
        this.creationTime = creationTime;
    }

    public String getCreatorUserId() {
        return creatorUserId;
    }

    public void setCreatorUserId(String creatorUserId) {
        this.creatorUserId = creatorUserId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RoleInfo roleInfo = (RoleInfo) o;
        return Objects.equals(id, roleInfo.id) &&
                Objects.equals(name, roleInfo.name) &&
                Objects.equals(sort, roleInfo.sort) &&
                Objects.equals(remark, roleInfo.remark) &&
                Objects.equals(organizationId, roleInfo.organizationId) &&
                Objects.equals(deletedFlag, roleInfo.deletedFlag) &&
                Objects.equals(deleterUserId, roleInfo.deleterUserId) &&
                Objects.equals(deletionTime, roleInfo.deletionTime) &&
                Objects.equals(lastModificationTime, roleInfo.lastModificationTime) &&
                Objects.equals(lastModifierUserId, roleInfo.lastModifierUserId) &&
                Objects.equals(creationTime, roleInfo.creationTime) &&
                Objects.equals(creatorUserId, roleInfo.creatorUserId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, sort, remark, organizationId, deletedFlag, deleterUserId, deletionTime, lastModificationTime, lastModifierUserId, creationTime, creatorUserId);
    }

    @Override
    public String toString() {
        return "RoleInfo{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", sort=" + sort +
                ", remark='" + remark + '\'' +
                ", organizationId='" + organizationId + '\'' +
                ", deletedFlag=" + deletedFlag +
                ", deleterUserId='" + deleterUserId + '\'' +
                ", deletionTime=" + deletionTime +
                ", lastModificationTime=" + lastModificationTime +
                ", lastModifierUserId='" + lastModifierUserId + '\'' +
                ", creationTime=" + creationTime +
                ", creatorUserId='" + creatorUserId + '\'' +
                '}';
    }
}
